package com.base.tool.ui;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.util.ArrayList;


public class Tool {
    private String TAG = "ActivityLauncher";


    public static void getActivity(Context context, ArrayList<ActivityInfo> mActivities) {

        //获取本应用的所有activity，排除本activity
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_ACTIVITIES);

            String ourName = context.getClass().getName();
            if (pi.activities.length > 0) {
                for (int i = 0; i < pi.activities.length; ++i) {
                    if (!ourName.equals(pi.activities[i].name)) {
                        mActivities.add(pi.activities[i]);
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            // Do nothing. Adapter will be empty.
        }

        Log.d("getActivity", "mActivities size =" + mActivities.size() + " activities=" + mActivities);
    }

    public static void removeActivityWithReg(ArrayList<ActivityInfo> mActivities, String reg) {
        ArrayList<ActivityInfo> del = new ArrayList<>();
        for (int i = 0; i < mActivities.size(); ++i) {
            if (!(mActivities.get(i).name.startsWith(reg))) {
                del.add(mActivities.get(i));
            }
        }
        mActivities.removeAll(del);

        Log.d("getActivity", "mActivities size =" + mActivities.size() + " activities=" + mActivities);
    }


    public static String getLaunchMode(int launchMode) {
        if (launchMode == 0) {
            return "standard";

        } else if (launchMode == 1) {
            return "singleTop";

        } else if (launchMode == 2) {
            return "singleTask:重用实例(会调用实例的 onNewIntent() )，会让该实例回到栈顶，因此在它上面的实例将会被移出栈";

        } else if (launchMode == 3) {
            return "singleInstance";

        }
        return "其它？？";
    }


}
